<?php
// +----------------------------------------------------------------------
// | 专利展示页（专利列表、专利详情、下单操作）
// +----------------------------------------------------------------------
// | Copyright (c) 2015
// +----------------------------------------------------------------------
// | Author: baiping 125618036@qq.com http://www.webipcode.com
// +----------------------------------------------------------------------
// |2015-12-9 下午2:12:01
// +----------------------------------------------------------------------

class Patent extends  MY_Controller{
	
	
	/**
	 * 专利首页
	 */
	public function index(){
		//推荐专利产品
		$patnet_common = $this->getPatent(array('state'=>0,'recommend'=>1), 6);
		//全部专利产品
		$patent = $this->getPatent(array('state'=>0));
		//获取专利类型下的子分类
		$this->getChildClass(173, 'patent_child_class');
		
		$this->load->vars('patent_common', $patnet_common);
		$this->load->vars('patent', $patent);
		$this->load->view('patent/index');
	}
	
	/**
	 * 专利产品详情
	 */
	public function detail(){
		$cat_id = $this->uri->segment(3, 0);
		$id = $this->uri->segment(4, 0);
		$status = 0;
		//如果传递了当前分类id而没有产品id，就获取当前分类下的最新的产品，否则列出当前的产品详情
		if ( $cat_id > 0 ){
			$this->load->model('Catgory_model', 'catgory');
			$catgory_info = $this->catgory->getData(0, array('pid'=>173) );
			$cargory_info_new = array();
			//加载专利模型
			$this->load->model('Patent_model', 'patent');
			foreach ( $catgory_info AS $key => $val ){
				$cargory_info_new[$val->id] = $val;
				//获取当前类别下所有的正常显示的产品
				$cargory_info_new[$val->id]->patent = $this->getPatent( array('cate_id'=>$val->id,'state'=>0) );
			}
			
			if ( $id > 0 ){
				//获取当前专利列表
				$patent_info = $this->patent->getFind(array('id'=>$id));
			}else{
				$patent_info = $this->patent->getFind(array('cate_id'=>$cat_id));
			}
			if ( !$patent_info ){
				$status = 1;
			//如果专利有官费类别不为空。刚获取专利官费类别	
			}elseif( !empty( $patent_info->office_type ) ){
				$this->load->model('Catgory_model', 'catgory');
				$catgory = $this->catgory->getField(array("id" => explode(',', $patent_info->office_type)), 'id,title');
			}
		}	
		
		$id = $id > 0 ? $id : $patent_info->id;
		$this->load->vars('status', $status);
		$this->load->vars('id', $id);
		$this->load->vars('cat_id', $cat_id);
		$this->load->vars('catgory_list', $cargory_info_new);//专利列表
		$this->load->vars('patent_info', $patent_info);//专利详情
		$this->load->vars('catgory', $catgory);//官费类别
		$this->load->vars('meta_title', $patent_info->title);
		$this->load->view('patent/detail');
	}
	
	/**
	 * 专利申请下单 
	 */
	public function apply(){
		$patent_id = $this->input->get('patent_id');
		$cat_id    = $this->input->get('cat_id', 0);
		$officer   = $this->input->get('officer');
		$success = 1;
		if ( empty( $patent_id ) || empty( $cat_id ) || empty( $officer ) ){
			$success = 0;
		}
		$info =  serialize( array($patent_id,$cat_id,$officer) );
		$this->load->vars('info', $info);
		$this->load->vars('success', $success);
		$this->load->view('patent/apply');
	}

	/**
	 * 处理专利申请下单处理
	 */
	public function applyed(){
		if ( IS_POST ){
			$patent_info = $this->input->post('patent_info');
			$verify =  $this->input->post('verify');
			if ( false == check_verify( $verify ) ){
				ajaxreturn(0, '验证码错误,请重新填写');
				return false;
			}
			if ( empty( $patent_info ) ){
				ajaxreturn(0, '参数错误,请返回重新操作', site_url('patent/index'));
			}
			$patent_info_arr = unserialize( $patent_info );//反序例化处理
			//通过产品的id和分类id获取专利产品详情
			$patent_info = $this->getPatentInfo( array('id'=>$patent_info_arr[0], 'cate_id'=>$patent_info_arr['1']) );
			//获取产品的服务费和官费
			$office_price_arr = unserialize( $patent_info->office_price );
			$service_price = $patent_info->service_price;
			$officer_price = $office_price_arr[ $patent_info_arr[2] ];
			$patent_price = array(
					'id'=>$patent_info_arr[0], 
					'cate_id'=>$patent_info_arr[1], 
					'office_id'=>$patent_info_arr[2],
					'service_price'=>$service_price, 
					'office_price'=>$officer_price
					);
			
			$this->load->model('Patent_order_model', 'order');
			$insert_id = $this->order->addData( $patent_price );
			if ( $insert_id ){
				ajaxreturn(1, '操作成功，等待业务人员与您联系', site_url('patent/detail/'. $patent_info_arr[1].'/'.$patent_info_arr[0]));
			}else{
				ajaxreturn(0,'操作失败');
			}
		}else{
			ajaxreturn(0, '非法请求');
		}	
	}
	
}